﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Utils;
namespace UI.UC
{
    public partial class TransDetail : System.Web.UI.UserControl
    {
        DataTable myDt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["StoreDataTable"] != null)
            {
                myDt = (DataTable)Session["StoreDataTable"];
            }
            else
            {
                myDt = CreateTable("Info");
                int row = 0;
                if (updateDataTable != null)
                {
                    row = updateDataTable.Rows.Count;
                    if (row > 0)
                    {
                        myDt = updateDataTable.Copy();
                    }
                }
                if (row < 6)
                    AddDummyData(myDt, 6 - row);
            }

            gvDetail.DataSource = myDt;
            gvDetail.DataBind();
        }
        private DataTable updateDataTable;
        public DataTable UpdateDataTable { get { return updateDataTable; } set { updateDataTable = value; } }

        private string[] getCode;
        public string[] GetCode { get { return getCode; } set { getCode = value; } }
       // private string msgInfo;
        public string MsgInfo { get; set; }

        public List<String[,]> DetailData
        {
            get
            {
                List<String[,]> detailList = new List<String[,]>();
               // MsgInfo = "";
                int rowNum = 0;//提示用的行号
                foreach (GridViewRow gvr in gvDetail.Rows)//检测
                {
                    rowNum++;
                    string productName = ((TextBox)gvr.FindControl("txtName")).Text.Trim();
                    string num = ((TextBox)gvr.FindControl("txtNum")).Text.Trim();
                    if (string.IsNullOrWhiteSpace(productName) && !RegexRule.IsNumeric(num))
                    {
                        continue;
                    }
                    string[,] item =
                    {
                   // {sheetCode[0].ToString(),sheetCode[1].ToString(),sheetCode[2].ToString()},
                   {getCode[0].ToString(),getCode[1].ToString(),getCode[2].ToString()},
                    {"ProductCode",((TextBox)gvr.FindControl("txtProductCode")).Text.Trim(),"S"},
                    //{"ProductType",((TextBox)gvr.FindControl("txtProductType")).Text.Trim(),"S"},

                    {"Name",productName,"S"},
                    {"Spec",((TextBox)gvr.FindControl("txtSpec")).Text.Trim(),"S"},
                    {"Num",num,"N"},
                   
                    {"Unit",((TextBox)gvr.FindControl("txtUnit")).Text.Trim(),"S"},
                    {"Profile",((TextBox)gvr.FindControl("txtProfile")).Text.Trim(),"S"},
                     {"RevMan",((TextBox)gvr.FindControl("txtRevMan")).Text.Trim(),"S"},
                      {"Memo",((TextBox)gvr.FindControl("txtMemo")).Text.Trim(),"S"}
                    };

                    if (item[2, 1].Trim()!="" && item[4, 1].Trim()!="" )//如果填写的名称，数量
                    {
                        //判断数值  num price
                        if (!RegexRule.IsNumeric(item[4, 1])) //Num
                        {
                            //数量必须输入数字
                            MsgInfo = "第" + rowNum + "行，数量必须输入数字！";
                            return null;
                        }
                        else
                        { 
                            detailList.Add(item);
                        }
                    }
                   // else
                    //{
                      //  MsgInfo = "第" + rowNum + "行，产品名称或数量没有填写";
                     //       return null;
                   // }
                }
                return detailList;
            }

        }
        /*
        * 
        * 添加空白行
        * 
        */
        private void AddDummyData(DataTable dt, int rowNum)
        {

            int begin = 0;
            if (dt != null) begin = dt.Rows.Count;
            for (int i = begin; i < rowNum + begin; i++)
            {
                DataRow dr = dt.NewRow();
                dr["TransCode"] = "";  
                dr["ProductCode"] = "";            
                dr["Name"] = "";
                dr["Spec"] = "";
                dr["Num"] = "1";               
                dr["Unit"] = "";
                dr["Profile"] = "";
                dr["RevMan"] = "";
                dr["Memo"] = "";
                dt.Rows.Add(dr);
            }

        }
        private DataTable FillGridviewData()
        {
            DataTable dt = CreateTable("Info");
            foreach (GridViewRow gvr in gvDetail.Rows)
            {
                DataRow dr = dt.NewRow();
                dr["ProductCode"] = ((TextBox)gvr.FindControl("txtProductCode")).Text;               
                dr["Name"] = ((TextBox)gvr.FindControl("txtName")).Text;
                dr["Spec"] = ((TextBox)gvr.FindControl("txtSpec")).Text;
                dr["Num"] = ((TextBox)gvr.FindControl("txtNum")).Text; 
                dr["Unit"] = ((TextBox)gvr.FindControl("txtUnit")).Text;
                dr["Profile"] = ((TextBox)gvr.FindControl("txtProfile")).Text;
                dr["RevMan"] = ((TextBox)gvr.FindControl("txtRevMan")).Text;
                dr["Memo"] = ((TextBox)gvr.FindControl("txtMemo")).Text;
                dt.Rows.Add(dr);

            }
            Session["StoreDataTable"] = dt;
            return dt;
        }
        /*
         * 
         *生成DataTable
         * 
         */
        private DataTable CreateTable(String className)
        {
            DataTable dt = new DataTable("StepLimitCondition");
            DataColumn column;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "TransCode";
            column.ColumnName = "TransCode";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "ProductCode";
            column.ColumnName = "ProductCode";
            dt.Columns.Add(column);

         
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Name";
            column.ColumnName = "Name";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Spec";
            column.ColumnName = "Spec";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Num";
            column.ColumnName = "Num";
            dt.Columns.Add(column);

            //column = new DataColumn();
            //column.DataType = System.Type.GetType("System.String");
            //column.Caption = "Price";
            //column.ColumnName = "Price";
            //dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Unit";
            column.ColumnName = "Unit";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Profile";
            column.ColumnName = "Profile";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "RevMan";
            column.ColumnName = "RevMan";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.Caption = "Memo";
            column.ColumnName = "Memo";
            dt.Columns.Add(column);


            return dt;
        }
        private DataTable AddARow(Int32 pos)//把一行数据添加到DatsSet中的DataTable
        {
            DataTable dt = FillGridviewData();
            if (dt.Rows.Count < 50)
            {
                DataRow dr = dt.NewRow();
                dr["TransCode"] = "";  
                dr["ProductCode"] = "";
                dr["Name"] = "";
                dr["Spec"] = "";
                dr["Num"] = "";
                dr["Unit"] = "";
                dr["Profile"] = "";
                dr["RevMan"] = "";
                dr["Memo"] = "";
                dt.Rows.InsertAt(dr, pos);
            }
            return dt;
        }
        private DataTable MoveARow(Int32 pos)//把一行数据添加到DatsSet中的DataTable
        {
            DataTable dt = FillGridviewData();
            if (dt.Rows.Count > 1)
            {
                dt.Rows.RemoveAt(pos - 1);
            }
            return dt;
        }
        protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {

                switch (e.CommandName)
                {
                    case "cmdDel":
                        String strID3 = e.CommandArgument.ToString();
                        DataTable dt3 = MoveARow(Int32.Parse(strID3));

                        gvDetail.DataSource = dt3;
                        gvDetail.DataBind();

                        break;
                    case "cmdAdd"://zj
                        String strID2 = e.CommandArgument.ToString();
                        DataTable dt2 = AddARow(Int32.Parse(strID2));

                        gvDetail.DataSource = dt2;
                        gvDetail.DataBind();

                        break;
                    default:

                        break;
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
        }

        protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbSN = (Label)e.Row.FindControl("lbSN");
                string strSN = (e.Row.DataItemIndex + 1).ToString();
                if (strSN.Length < 2) strSN = "0" + strSN;
                lbSN.Text = strSN;//在前面加一列序号                // e.Row.Cells[0].Text = 


                LinkButton lbtnDel = (LinkButton)e.Row.FindControl("lbtnDel");
                LinkButton lbtnAdd = (LinkButton)e.Row.FindControl("lbtnAdd");

                lbtnDel.CommandArgument = (e.Row.DataItemIndex + 1).ToString();
                lbtnAdd.CommandArgument = (e.Row.DataItemIndex + 1).ToString();
            }
        }



    }
}